import pandas as pd
from SaftyAssessment import CalEvaculationTime
import TempIO
import os
import shapefile
import numpy as np

'''
Author: Boliang Dong
Date: 2021-11-29 17:03:05
LastEditTime: 2021-12-01 16:56:25
LastEditors: Please set LastEditors
Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
FilePath: \flood-evacuation-risk-assessment\FERA.py
'''


def main():
    print("Calculate the Flood Evacuation Risks!!!")
    pointName = "Point.txt"
    lineName = "Line.shp"
    DepthName = "Depth.txt"
    VelocityName = "Velocity.txt"
    escapeName = "EscapeRoutine.txt"
    safeName = "HumanSafty.json"

    #制定文件夹路径
    currentPath =  os.getcwd()
    pointName = os.path.join(currentPath, "Input", pointName)
    DepthName = os.path.join(currentPath, "Input", DepthName)
    lineName = os.path.join(currentPath, "Input", lineName)
    VelocityName = os.path.join(currentPath, "Input", VelocityName)
    safeName = os.path.join(currentPath, "Input", safeName)
    escapeName = os.path.join(currentPath, "Input", escapeName)
    #读取计算点水位流速随时间变化情况
    pointData = TempIO.read_character_point_hydrodynamic_data(pointName)
    depthData = TempIO.read_character_point_hydrodynamic_data(DepthName)
    velocityData = TempIO.read_character_point_hydrodynamic_data(VelocityName)
    print(depthData)
    #读取线条
    #lines = shapefile.Reader(lineName)
    #lineData = TempIO.convertShpToPandas(lines)

    #读取逃生路线
    escapeData = []
    with open(escapeName) as file:
        for line in file:
            templist = line.split()
            outlist = [int(i) for i in templist]
            escapeData.append(outlist)

    #计算逃生路线每段的长度与起始点
    escapeLenth = []
    escapePoint = []
    for item in escapeData:
        temp1 = []
        temp2 = []
        for i in range(len(item)-1):
            stNode = item[i]
            edNode = item[i+1]
            temp1.append([stNode, edNode])
            temp2.append(np.sqrt((pointData.loc[stNode,"x"]-pointData.loc[edNode,"x"])**2+(pointData.loc[stNode,"y"]-pointData.loc[edNode,"y"])**2))
        escapeLenth.append(temp2)
        escapePoint.append(temp1)
    
    #计算逃生时间
    Results = CalEvaculationTime(depthData, velocityData, escapePoint, escapeLenth, safeName)
    Results = pd.DataFrame(Results)
    Index = [i*10 for i in range(0, Results.shape[0])]
    Results.loc[:,"Time"] = Index
    Results.to_csv("EscapeTime.csv")
    print("Successfully Finished!!!")
if __name__ == "__main__":
    main()




